package light.head.controller.inv;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.inv.InvBillCodeDAO;
import light.head.dao.inv.InvBillConfingDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.inv.InvBillCode;
import light.head.model.inv.InvBillCodeConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 单据生成配置
 * @author yangguanghe @date 2014-6-17 @time 下午3:17:18
 *
 */
@Controller
@RequestMapping(Url.INV_BILLCONFING)
public class InvBillConfingController extends SupporterController {
	
	private InvBillConfingDAO invBillConfingDAO;
	private OrganDAO organDAO;
	private InvBillCodeDAO ibcDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init (HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.INV_BILLCONFING_INIT); 
	}

	@RequestMapping(Url.LIST)
	public ModelAndView list (HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> map = createQueryMapByParmName(request, Parm.DELETED);
		PageBean pb = createPageBean(request);
		invBillConfingDAO.findByMap(cu_organCode(request), pb, map);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.INV_BILLCONFING_LIST); 
	}
	@RequestMapping(Url.EDIT)
	public ModelAndView edit (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) return new ModelAndView(Url.INV_BILLCONFING_EDIT);
		
		InvBillCodeConfig b = invBillConfingDAO.get(Integer.parseInt(code));
		if (null == b) return new ModelAndView(Url.INV_BILLCONFING_EDIT); 
		
		return new ModelAndView(Url.INV_BILLCONFING_EDIT); 
	}
	
	@Transactional(rollbackFor = Exception.class)
	@RequestMapping(Url.SAVE)
	public void save (InvBillCodeConfig ibc, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		if (null == ibc) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		if (null != ibc.getCode()) {
			if (RESULT.SUCCESS != invBillConfingDAO.update(ibc))
				print(response, Msg.PARAMETER_ERROR);
			
			// 防止只有配置没有计数器的数据
			InvBillCode dbBcode = ibcDAO.getByBcc(ibc.getCode());
			if (null == dbBcode) {
				InvBillCode bcode = new InvBillCode();
				bcode.setBcc(ibc.getCode());
				bcode.setNumber(1);
				bcode.setYmd(new SimpleDateFormat(Sys.billCode_ymd).format(new Date()));
				if (RESULT.SUCCESS != ibcDAO.add(bcode))
					throw new Exception("为单据配置(InvBillCodeConfig) code:" + ibc.getCode() + "添加计数器失败");
			}
		} else {
			
			ibc.setCu(cu_code(request));
			ibc.setCt(new Date());
			ibc.setDeleted(false);
			ibc.setOc(cu_organCode(request));
			
			if (RESULT.SUCCESS != invBillConfingDAO.add(ibc))
				throw new Exception("保存单据配置错误");
			
			InvBillCode bcode = new InvBillCode();
			bcode.setBcc(ibc.getCode());
			bcode.setNumber(1);
			bcode.setYmd(new SimpleDateFormat(Sys.billCode_ymd).format(new Date()));
			
			if (RESULT.SUCCESS != ibcDAO.add(bcode))
				throw new Exception("保存单据计数器错误");
			
		}
		
		print(response, Msg.SUCCESS);
		
	}
	
	@RequestMapping(Url.SHIELD)
	public void shield (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		InvBillCodeConfig ibcc = invBillConfingDAO.get(Integer.parseInt(code));
		if (null == ibcc) {
			print(response, Msg.NOT_FIND);
			return;
		}
		
		ibcc.setDeleted(true);
			
		if (RESULT.SUCCESS == invBillConfingDAO.upd(ibcc))
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
		
	}
	
	@RequestMapping(Url.UN_SHIELD)
	public void unShield (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		InvBillCodeConfig ibcc = invBillConfingDAO.get(Integer.parseInt(code));
		if (null == ibcc) {
			print(response, Msg.NOT_FIND);
			return;
		}
		
		ibcc.setDeleted(false);
			
		if (RESULT.SUCCESS == invBillConfingDAO.upd(ibcc))
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}

	/** 测试 生成单据编号 **/
	@Transactional(rollbackFor = Exception.class)
	@RequestMapping(Url.LOAD_TOTAL)
	public void billCode (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String no = invBillConfingDAO.createBCode(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_TWO);
		
		System.out.println(no);
		
		
		
	}


	@Autowired
	public void setInvBillConfingDAO(InvBillConfingDAO invBillConfingDAO) {
		this.invBillConfingDAO = invBillConfingDAO;
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}

	@Autowired
	public void setIbcDAO(InvBillCodeDAO ibcDAO) {
		this.ibcDAO = ibcDAO;
	}

	
	
}
